export const getStyle = (el, attr) => {
  // 简单判断ie6~8
  let ie = !+'\v1'
  if (attr === 'backgroundPosition') {
    // IE6~8不兼容backgroundPosition写法，识别backgroundPositionX/Y
    if (ie) {
      return el.currentStyle.backgroundPositionX + ' ' + el.currentStyle.backgroundPositionY
    }
  }
  if (el.currentStyle) {
    return el.currentStyle[attr]
  } else {
    return document.defaultView.getComputedStyle(el, null)[attr]
  }
}

export const setStyle = (el, styleName, value) => {
  if (el === undefined | styleName === undefined) {
    return
  }
  // 数组
  if (typeof styleName === 'object') {
    for (let key in styleName) {
      el.style[key] = styleName[key]
    }
  } else {
    el.style[styleName] = value
    console.log(styleName, value)
  }
}

export function hasClass(el, className) {
  let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  return reg.test(el, className)
}

export function addClass(el, className) {
  if (hasClass(el, className)) {
    return false
  }
  let newClass = el.className.split(' ')
  if (typeof className === 'object') {
    newClass.push(...className)
  } else {
    newClass.push(className)
  }
  el.className = newClass.join(' ')
}
